package main

import (
	"encoding/json"
	"errors"
	"fmt"

	"github.com/hyperledger/fabric/core/chaincode/shim"
)
//用户
type MerCust struct {
	merId      string   //商户号
	merCustId  string   //商户用户号
	userId     string   //业务线用户号
	accountId  string   //账户用户号
	balance    float64  //余额
}
//标的用户
type MerProduct struct {
	merId          string   //商户号
	merProjectId   string   //标的号
	projectName    string   //标的名称
	projectAmount  string   //标的金额
	loanUserId     string   //标的融资人资金账户托管平台用户号
	balance        float64  //余额
}
//交易
type Transaction struct {
	transactionId   string   //交易号
	transactionType string   //交易类型
	transactionTime string   //交易时间
	amount          float64  //交易金额
	from            string   //交易输入
	to              string   //交易输出
	message         string   //交易说明
}

//交易相关
type TransactionRelevant struct {
	relevantId        string  //相关交易号
	transactionId     string  //交易号
	transactionTime   string  //交易时间
	amount            float32 // 交易金额
}
//简单的链上代码
type SimpleChaincode struct {
}

//invoke 
func (t *SimpleChaincode) invoke (stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
	if function == "addMerCust" {
		t.addMerCust(stub, args)
	} else if function == "addMerProduct"{
		t.addMerProduct(stub, args)
	}

}

//添加用户
func (t *SimpleChaincode)  addMerCust(stub *shim.ChaincodeStub, args []string) ([]byte, error){
	var mercust MerCust
	var mercustBytes  []byte
	mercust = MerCust{merId:args[0], merCustId:args[1], userId:args[2], accountId:args[3], balance:0}
	err := writeMercust(stub, mercust)
	if err != nil {
		return nil, errors.New("write Error" + err)
	}
	mercustBytes, err = json.Marshal(&mercust)
	if err != nil {
		return nil, errors.New("Error retrieving mercustBytes")
	}
	return mercustBytes,nil
}

//写入用户
func writeMercust (stub *shim.Chaincode, mercust MerCust) (error) {
	mcBytes, err := json.Marshal(&mercust)
	if err != nil {
		return err
	}
	err = stub.PutState("MerCust", mcBytes)
	if err != nil {
		return errors.New("PutState Error" + err)
	}
	return nil
}

//添加标底用户
func (t *SimpleChaincode) addMerProduct (stub *shim.Chaincode, args []string ) ([]byte, error ) {
	var merproduct MerProduct
	var merproductBytes []byte
	merproduct = MerProduct{merId:args[0], merProjectId:args[1], projectName:args[2], projectAmount:args[3], loanUserId:args[4], balance:0}
	err := writeMerProduct(stub, merproduct)
	if err != nil {
		return nil, errors.New("write Error" + err)
	}
	merproductBytes, err = json.Marshal(&merproduct)
	if err != nil {
		return nil, errors.New("Error retrieving merproductBytes")
	}
	return merproductBytes, nil
}
//写入标底用户
func writeMerProduct(stub *shim.Chaincode, merproduct MerProduct) (error) {
	mpByte, err := json.Marshal(&merproduct)
	if err != nil {
		return err
	}
	err = stub.PutState("MerProduct", mpByte)
	if err != nil {
		return errors.New("PutState Error" + err)
	}
	return nil
}

func main() {
	err := shim.Start(new(SimpleChaincode))
	if err != nil {
		fmt.Printf("Error starting Simple chaincode: %s", err)
	}
}